<?php 
require_once("class.airport_info.php");
function compare_priorities($a,$b)
{
 if($a["priority"]<$b["priority"])
 {
     return -1;
 }
 else if($a["priority"]>$b["priority"])   
 {
     return 1;
 }
 else
 {
     return 0;
 }
}
  class request_fetcher
  {
      var $con;
      var $request_arr=array();
      var $dep_info;
      var $ret_info;
      var $config_id;
      var $config_arr=array();
      public function request_fetcher()
      {
          $this->con=mysql_connect("64.50.168.106","cheap39_citcd","citcd");
          mysql_select_db("test",$this->con);
      }
     
      public function get_request_index($dep_arp,$ret_arp)
      {
          $this->dep_info=$this->get_info($dep_arp);
          $this->ret_info=$this->get_info($ret_arp);
          //var_dump($dep_info);
          //var_dump($arr_info);
          $this->search_by_arp_code($this->dep_info,$this->ret_info);
      }
      
      private function get_info($arp_code)
      {
          $q_r="SELECT * FROM airport_code WHERE airport_code='".$arp_code."'";
          $res= mysql_query($q_r);
          $result=mysql_fetch_array($res);
          $info= new airport_info($result["region_code"],$result["airport_code"],$result["state_code"],$result["country_code"],$result["name"]);
          return $info;
      }
      
      public function search_by_codes($dep_code,$dep_type,$arr_code,$arr_type)
      {
          if($dep_type==1&&$arr_type==1)
          {
              $this->search_by_arp_code($this->get_info($dep_code),$this->get_info($arr_code));
          }else if($dep_type==1&&$arr_type==2)
          {
             $this->search_by_arp_code($this->get_info($dep_code),$this->get_state_info($arr_code));
             
          }
          else if($dep_type==1&&$arr_type==3)
          {
              $this->search_by_arp_code($this->get_info($dep_code),$this->get_country_info($arr_code));
          }else if($dep_type==1&&$arr_type==4)
          {
              $this->search_by_arp_code($this->get_info($dep_code),$this->get_region_info($arr_code));
          }else if($dep_type==2&&$arr_type==1)
          {
              $this->search_by_arp_code($this->get_state_info($dep_code),$this->get_info($arr_code));
          }else if($dep_type==2&&$arr_type==2)
          {
             $this->search_by_arp_code($this->get_state_info($dep_code),$this->get_state_info($arr_code));
             
          }
          else if($dep_type==2&&$arr_type==3)
          {
              $this->search_by_arp_code($this->get_state_info($dep_code),$this->get_country_info($arr_code));
          }else if($dep_type==2&&$arr_type==4)
          {
              $this->search_by_arp_code($this->get_state_info($dep_code),$this->get_region_info($arr_code));
          }else if($dep_type==3&&$arr_type==1)
          {
              $this->search_by_arp_code($this->get_country_info($dep_code),$this->get_info($arr_code));
          }else if($dep_type==3&&$arr_type==2)
          {
             $this->search_by_arp_code($this->get_country_info($dep_code),$this->get_state_info($arr_code));
             
          }
          else if($dep_type==3&&$arr_type==3)
          {
              $this->search_by_arp_code($this->get_country_info($dep_code),$this->get_country_info($arr_code));
          }else if($dep_type==3&&$arr_type==4)
          {
              $this->search_by_arp_code($this->get_country_info($dep_code),$this->get_region_info($arr_code));
          }else if($dep_type==4&&$arr_type==1)
          {
              $this->search_by_arp_code($this->get_region_info($dep_code),$this->get_info($arr_code));
          }else if($dep_type==4&&$arr_type==2)
          {
             $this->search_by_arp_code($this->get_region_info($dep_code),$this->get_state_info($arr_code));
             
          }
          else if($dep_type==4&&$arr_type==3)
          {
              $this->search_by_arp_code($this->get_region_info($dep_code),$this->get_country_info($arr_code));
          }else if($dep_type==4&&$arr_type==4)
          {
              $this->search_by_arp_code($this->get_region_info($dep_code),$this->get_region_info($arr_code));
          }
          
      }
      
      
      private function get_state_info($st_code)
      {
          $str="SELECT DISTINCT country_code,region_code FROM airport_code WHERE state_code='".$st_code."'";
          $res=mysql_query($str);
          $result=mysql_fetch_object($res);
          $arp_in= new airport_info($result->region_code,'',$st_code,$result->country_code,'');
          
          return $arp_in;
          
          
      }
      private function get_country_info($con_code)
      {
          $str="SELECT DISTINCT region_code FROM airport_code WHERE country_code='".$con_code."'";
          $res=mysql_query($str);
          $result=mysql_fetch_object($res);
          $arp_in= new airport_info($result->region_code,'','',$con_code,'');
          
          return $arp_in;
      }
      private function get_region_info($reg_code)
      {
              $arp_in= new airport_info($reg_code,'','','','');
              return $arp_in;
      }
      
      private function search_by_arp_code($dep,$arr)
      {
       $str="SELECT * FROM configuration WHERE 
       (dep_code='".$dep->airport_code."' AND arr_code='".$arr->airport_code."' ) 
       OR (dep_code='".$dep->airport_code."' AND arr_code='".$arr->state."' )
       OR (dep_code='".$dep->state."' AND arr_code='".$arr->state."' ) 
       OR (dep_code='".$dep->airport_code."' AND arr_code='".$arr->country."' )
       OR (dep_code='".$dep->country."' AND arr_code='".$arr->airport_code."' )
       OR (dep_code='".$dep->airport_code."' AND arr_code='".$arr->region."' )
       OR (dep_code='".$dep->region."' AND arr_code='".$arr->airport_code."' )
       OR (dep_code='".$dep->state."' AND arr_code='".$arr->state."' )
       OR (dep_code='".$dep->state."' AND arr_code='".$arr->country."' )
       OR (dep_code='".$dep->country."' AND arr_code='".$arr->state."' )
       OR (dep_code='".$dep->state."' AND arr_code='".$arr->region."' )
       OR (dep_code='".$dep->region."' AND arr_code='".$arr->state."' )
       OR (dep_code='".$dep->country."' AND arr_code='".$arr->region."' )
       OR (dep_code='".$dep->region."' AND arr_code='".$arr->country."' )
       OR (dep_code='".$dep->region."' AND arr_code='".$arr->region."' )
       OR (dep_code='".$dep->country."' AND arr_code='".$arr->country."' )";
       echo $str;
       $res=mysql_query($str)or die(mysql_error());
       $arr=array();
       while($result=mysql_fetch_array($res))
       {
           $arr[]=$result;
       }
       usort($arr,"compare_priorities");
       $this->config_id=$arr[0]["idConfiguration"];
       $this->config_arr=$arr;
       $str2="SELECT * FROM requests WHERE config_id='".$this->config_id."'"; 
       $res2=mysql_query($str2);
       $arr2=array();
       while($result2=mysql_fetch_array($res2))
       {
           $arr2[]=$result2;
       }
        $this->request_arr=$arr2;
      }
  }
?>
